//
//  CommunityChangeVC.swift
//  Employee
//
//  Created by tancheng on 2018/6/26.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

class CommunityChangeVC: BaseVC {
    
    
    @IBOutlet weak var currentCommunity: UILabel!
    @IBOutlet weak var tableView: UITableView!
    
    fileprivate var communitys:[OrgModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        getData()
    }
    
    func setupUI() {
        
        title = "社区切换"
        
        currentCommunity.text = "当前社区 " + User.communityName//
        
        tableView.register(UINib.init(nibName: "CommunityChangeCell", bundle: nil), forCellReuseIdentifier: CommunityChangeCell.reuseIdentifer)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = GlobalConfig.pageColor
    }
    
    func getData() {
        
        UserRequest.shared.getOrgData(loginSuccess: { (data) in
            self.communitys += data
            self.tableView.reloadData()
        })
            
    }
    
    func updateUserInfo(_ idx: Int) {
        
        guard let infoDic = User.userInfoDic else {
            showHint("切换失败")
            return
        }
        
        if let pastCompanyId =  infoDic["companyId"] as? String{
            guard pastCompanyId != self.communitys[idx].companyId else {
                showHint("您已切换至当前社区")
                return
            }
        }
        
        
        // 保存用户信息
        let newData = NSMutableDictionary.init(dictionary: infoDic)
        
        newData["communityName"] = self.communitys[idx].communityName//
        newData["companyId"] = self.communitys[idx].companyId//
        
        User.save(with: JSON.init(newData))
        
        SJApp.showHUD(in: view)
         UserRequest.shared.getUserInfo(loginSuccess: {
            
            self.currentCommunity.text = "当前社区 \(self.communitys[idx].communityName)"
            //
            NotificationCenter.default.post(name: NSNotification.Name.changeCompany.changeCompanyUpdateData, object: nil, userInfo: nil)
            
            self.navigationController?.popViewController(animated: true)
            
         }, failure: {
            
            self.showHint("切换失败")
            
         })
        
        
        
        
        
    }
    
    
    
}

extension CommunityChangeVC: UITableViewDelegate, UITableViewDataSource {
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return communitys.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 125
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CommunityChangeCell.reuseIdentifer, for: indexPath) as! CommunityChangeCell
        cell.communityName.text = communitys[indexPath.row].communityName
        cell.index = indexPath.row
        cell.changeBlock = {[weak self] index in
            //切换
           
            self?.updateUserInfo(index)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    
}
